package com.itic.ht.mem.performance.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.mem.member.persistence.dao.WebMemberDao;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.persistence.model.WebMemberExample;
import com.itic.ht.mem.performance.persistence.dao.PerformanceDao;
import com.itic.ht.mem.performance.persistence.model.Performance;
import com.itic.ht.mem.performance.persistence.model.PerformanceExample;

import sun.java2d.loops.DrawGlyphListAA.General;

@Service
@Transactional
public class PerformanceService {
	
	@Autowired
	private PerformanceDao performanceDao;
	@Autowired
	private WebMemberDao webMemberDao;
	
	protected static final String SUCCESS = ResponseCode.SUCCESS.getCode();
	protected static final String FAILED = ResponseCode.SERVICE_ERROR.getCode();
	public Performance get(String id) {
		if (StringHelper.isNotEmpty(id)) {
			return performanceDao.selectByPrimaryKey(id);
		}
		return null;
	}

	public int count(PerformanceExample example) {
		return performanceDao.countByExample(example);
	}

	public String add(Performance templet) {
		//判断用户是否是第一次收益
		int code = 0;
		if(StringHelper.isNotBlank(templet.getMemAccount())){
			PerformanceExample example = new PerformanceExample();
			example.createCriteria().andMemAccountEqualTo(templet.getMemAccount()).andEffectiveEqualTo(DBConst.TRUE);
			List<Performance> list = performanceDao.selectByExample(example);
			if(null!=list&&list.size()>0){
				Performance performance = list.get(0);
				performance.setIncome(templet.getIncome()+performance.getIncome());
				code=performanceDao.updateByExampleSelective(performance, example);
			}else{
				templet.setId(IdGen.uuid());
				templet.setCtime(new Date());
				templet.setMtime(new Date());
				templet.setEffective(DBConst.TRUE);
				code = performanceDao.insert(templet);
			}
		}
		if (code > 0) {
			return SUCCESS;
		} else {
			return FAILED;
		}
	}

	public String delete(Performance temp) {
		int code = 0;
        String id = temp.getId();
        if (StringHelper.isNotEmpty(id.toString().trim())) {
            if (null == temp.getMtime()) {
            	temp.setMtime(new Date());
            }
            temp.setEffective(DBConst.FALSE);
            code = performanceDao.updateByPrimaryKeySelective(temp);
        }
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }	
	}

	public List<Performance> list(PerformanceExample example) {
		return performanceDao.selectByExample(example);
	}

	public String edit(Performance templet) {
		 int code = 0;
	        String id = templet.getId();
	        if (StringHelper.isNotEmpty(id.toString().trim())) {
	            if (null == templet.getMtime()) {
	            	templet.setMtime(new Date());
	            }
	            code = performanceDao.updateByPrimaryKeySelective(templet);
	        }
	        if (1 == code) {
	            return SUCCESS;
	        } else if (0 == code) {
	            return FAILED;
	        } else {
	            return FAILED;
	        }
	}

	public Performance getPerformanceByAccount(String account) {
		Performance performance = null;
		try {
			PerformanceExample example = new PerformanceExample();
			example.createCriteria().andMemAccountEqualTo(account).andEffectiveEqualTo(DBConst.TRUE);
			List<Performance> performances = performanceDao.selectByExample(example);
			System.out.println("用户信息个数:"+performances.size());
			performance = performances.get(0);
			return performance;
		} catch (Exception e) {
			return null;
		}
	}

}
